package com.zmzncs.lmtc.module.customer.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.customer.pojo.cust_ticket.CustTicketDTO;
import com.zmzncs.lmtc.module.customer.pojo.cust_ticket.CustTicketGrantForm;
import com.zmzncs.lmtc.module.customer.pojo.cust_ticket.CustTicketShopSelectForm;
import com.zmzncs.lmtc.module.customer.pojo.cust_ticket.CustTicketShopTableVO;
import com.zmzncs.lmtc.module.customer.service.CustTicketService;
import com.zmzncs.lmtc.module.shop.pojo.shop_order.ShopOrderDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_order.ShopOrderTableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-13
 */
@Slf4j
@Api(tags = "用户优惠券信息")
@RestController
@RequestMapping("/system/cust-ticket")
public class CustTicketController {

    @Autowired
    private CustTicketService custTicketService;

    @ApiOperation(value = "条件分页查询优惠券使用详情")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="pageNum", value="页码", required = true),
            @ApiImplicitParam(paramType="query", name="pageSize", value="页长", required = true),
            @ApiImplicitParam(paramType="query", name="ticketId", value="优惠券ID"),
    })
    @PostMapping(value = "getTicketUseInfo")
    public Rest<Map<String,Object>> getTicketUseInfo(@NotNull Integer pageNum, @NotNull Integer pageSize, @NotNull Long ticketId) {
        Map<String,Object> map = custTicketService.getTicketUseInfo(new Page<>(pageNum, pageSize), ticketId);

        return RestUtil.success(map);
    }

    @ApiOperation(value = "发放优惠券")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "grantTicket")
    public Rest grantTicket(@RequestBody CustTicketGrantForm form) {
        custTicketService.grantTicket(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "分页查询商户优惠券领取情况")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "getShopTicketPage")
    public Rest<PageInfo<CustTicketShopTableVO>> getShopTicketPage(@Valid CustTicketShopSelectForm form) {

        IPage<CustTicketDTO> iPage = custTicketService.getShopTicketPage(form);
        PageInfo<CustTicketShopTableVO> pageInfo = JsonUtil.pageTransform(iPage, CustTicketShopTableVO.class);

        return RestUtil.success(pageInfo);
    }

}

